using System;
using System.Windows.Input;

namespace CuteConsole.Mvvm
{
  /// <summary>
  /// This ViewModelBase subclass requests to be removed 
  /// from the UI when its CloseCommand executes.
  /// This class is abstract.
  /// </summary>
  internal abstract class WindowViewModel : ViewModelBase
  {
    private RelayCommand _closeCommand;

    /// <summary>
    /// Returns the command that, when invoked, attempts
    /// to remove this workspace from the user interface.
    /// </summary>
    public ICommand CloseCommand
    {
      get
      {
        if (_closeCommand == null)
        {
          _closeCommand = new RelayCommand(param => Close());
        }

        return _closeCommand;
      }
    }

    /// <summary>Raised when this workspace should be removed from the UI.</summary>
    public event EventHandler RequestClose;

    public void Close()
    {
      InvokeRequestClose();
      Dispose();
    }

    protected virtual void InvokeRequestClose()
    {
      var handler = RequestClose;
      if (handler != null)
        handler(this, EventArgs.Empty);
    }
  }
}